package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.client.entity.Applicant;
import cn.lg.soar.boot.client.entity.ClientUser;
import cn.lg.soar.boot.client.service.ApplicantService;
import cn.lg.soar.boot.client.service.ClientUserService;
import cn.lg.soar.boot.fc.entity.*;
import cn.lg.soar.boot.fc.entity.Process;
import cn.lg.soar.boot.fc.service.*;
import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.ParameterUtil;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.system.auth.entity.User;
import cn.lg.soar.system.auth.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.fc.mapper.FormResultMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 表单项目提交结果表
 * @author ZhuanZ（无密码）
 * @since 2025-01-17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class FormResultServiceImpl extends BaseServiceImpl<FormResultMapper, FormResult> implements FormResultService {

    @Autowired
    protected IUserService userService;
    @Autowired
    protected FormCollectService formCollectService;
    @Resource
    private ProcessService processService;
    @Resource
    private ProcessApplyService processApplyService;
    /** 客户端用户信息服务 */
    @Autowired private ClientUserService clientUserService;
    @Autowired
    private ApplicantService applicantService;

    private SqueryWapper<FormResult> squeryWapper;

    /**
     * 用户提交表单
     */
    @Transactional
    public HttpResult<Void> submitForm(FormResult addForm,Long roomId,Long leaseId) {
        ParameterUtil.notNull(addForm, "数据不能为空");
//        User user = userService.getById(addForm.getSubmitUserId());
//        if (null == user){
//            System.out.println("===============1申请用户为空！");
//            return HttpResult.fail("B0000", I18n.build("申请用户为空！"));
//        }
        ClientUser user = clientUserService.getById(addForm.getSubmitUserId());
        if(user == null) {
            return HttpResult.fail("B0000", I18n.build("用户为空！"));
        }
        Applicant applicant = applicantService.getById(addForm.getSubmitApplicantId());
        if(applicant == null) {
            return HttpResult.fail("B0000", I18n.build("申请人为空！"));
        }
        FormCollect formCollect = formCollectService.getById(addForm.getCollectId());
        if (null == formCollect){
            System.out.println("===============1表单收集为空！");
            return HttpResult.fail("B0000", I18n.build("表单收集为空！"));
        }
        LocalDateTime now = LocalDateTime.now();
        addForm.setSubmitTime(now);
        AssertUtil.isTrue(super.save(addForm), "创建失败");
        if(formCollect.getIsAuditing() == 1){
            Process process = processService.getById(formCollect.getProcessId());
            if (null == process){
                System.out.println("===============1流程应用为空！");
                return HttpResult.fail("B0000", I18n.build("流程应用为空！"));
            }

            ProcessApply apply = new ProcessApply();
            apply.setCollectId(formCollect.getId());
            apply.setCustomFormId(addForm.getId());
            apply.setUserId(user.getId());
            apply.setUserName(user.getUserName());
            apply.setApplicantId(applicant.getId());
            apply.setApplicantName(applicant.getName());
            apply.setProcessId(process.getId());
            apply.setProcessName(process.getProcessName());
            return processApplyService.apply(apply,roomId,leaseId);
        }
        return HttpResult.success();
    }

    /**
     * 用户提交基础信息表单
     */
    @Transactional
    public HttpResult<Void> submitUserForm(FormResult addForm,String name,String phone,Integer userLink) {
        ParameterUtil.notNull(addForm, "数据不能为空");
        ClientUser user = clientUserService.getById(addForm.getSubmitUserId());
        if(user == null) {
            return HttpResult.fail("B0000", I18n.build("申请用户为空！"));
        }
        FormCollect formCollect = formCollectService.getById(addForm.getCollectId());
        if (null == formCollect){
            System.out.println("===============1表单收集为空！");
            return HttpResult.fail("B0000", I18n.build("表单收集为空！"));
        }
        LocalDateTime now = LocalDateTime.now();
        addForm.setSubmitTime(now);
        if(addForm.getId() == null){
            AssertUtil.isTrue(super.save(addForm), "创建失败");
        }else{
            AssertUtil.isTrue(super.updateById(addForm), "创建失败");
        }
        if(formCollect.getIsAuditing() == 1){
            addForm = super.getById(addForm.getId());
            Applicant applicant = new Applicant();
            if(addForm.getSubmitApplicantId() != null){
                applicant = applicantService.getById(addForm.getSubmitApplicantId());
                applicant.setName(name);
                applicant.setPhone(phone);
                applicant.setUserLink(userLink);
                applicant.setFormResultId(addForm.getId());
                applicant.setResult(0);
                AssertUtil.isTrue(applicantService.updateById(applicant), "创建失败");
            }else{
                applicant.setName(name);
                applicant.setPhone(phone);
                applicant.setUserLink(userLink);
                applicant.setUserId(user.getId());
                applicant.setFormResultId(addForm.getId());
                applicant.setResult(0);
                AssertUtil.isTrue(applicantService.save(applicant), "创建失败");
            }
            addForm.setSubmitApplicantId(applicant.getId());
            AssertUtil.isTrue(super.updateById(addForm), "创建失败");

            Process process = processService.getById(formCollect.getProcessId());
            if (null == process){
                return HttpResult.fail("B0000", I18n.build("流程应用为空！"));
            }
            ProcessApply apply = new ProcessApply();
            apply.setCollectId(formCollect.getId());
            apply.setCustomFormId(addForm.getId());
            apply.setUserId(user.getId());
            apply.setUserName(user.getUserName());
            apply.setApplicantId(applicant.getId());
            apply.setApplicantName(applicant.getName());
            apply.setProcessId(process.getId());
            apply.setProcessName(process.getProcessName());
            return processApplyService.apply(apply,null,null);
        }
        return HttpResult.success();
    }

}
